package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.SearchKeyWords;
import com.le123.ysdq.R;

public class SearchKeywordListAdapter extends MyBaseAdapter {

	private SearchKeyWords keywordList;
	private String type;	//type用于区别是联想词还是搜索历史
	private boolean isHasFooterView = false;
	
	private class KeywordHolder {
		TextView keyword;
		ImageView leftIv;
		View divider;
	}
	
	public SearchKeywordListAdapter(Context context,SearchKeyWords keywordList, String type) {
		super(context);
		// TODO Auto-generated constructor stub
		this.keywordList=keywordList;
		this.type = type;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return keywordList.getWords().size();
	}
	public void clear(){
		keywordList.getWords().clear();
		notifyDataSetChanged();
	}
	
	public void resetData(SearchKeyWords words, String type) {
		keywordList = words;
		this.type = type;
		notifyDataSetChanged();
	}

	public void resetData(SearchKeyWords words, String type, boolean isHasFooterView) {
		keywordList = words;
		this.type = type;
		this.isHasFooterView = isHasFooterView;
		notifyDataSetChanged();
	}

	public void isAddFooterView(boolean hasFooterView) {
		this.isHasFooterView = hasFooterView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		KeywordHolder holder = new KeywordHolder();
		if(convertView==null){
			convertView=inflater.inflate(R.layout.search_keyword_item, null);
			holder.keyword = (TextView) convertView.findViewById(R.id.search_keyword);
			holder.leftIv = (ImageView) convertView.findViewById(R.id.search_item_left_iv);
			holder.divider = convertView.findViewById(R.id.divider);
			convertView.setTag(holder);
		}
		else{
			holder=(KeywordHolder) convertView.getTag();
		}
		if("suggest".equalsIgnoreCase(type)){
			holder.leftIv.setVisibility(View.VISIBLE);
			if(!TextUtils.isEmpty(keywordList.getSearchKey())){

			}
		}else{
			holder.leftIv.setVisibility(View.GONE);
		}
		if(!TextUtils.isEmpty(keywordList.getSearchKey())){
			String txt = keywordList.getWords().get(position);
			String searchKey = keywordList.getSearchKey();
			if (txt != null && txt.contains(searchKey)) {

				int index = txt.indexOf(searchKey);

				int len = searchKey.length();

				Spanned temp = Html.fromHtml(txt.substring(0, index)
						+ "<font color=#3599f8>"
						+ txt.substring(index, index + len) + "</font>"
						+ txt.substring(index + len, txt.length()));

				holder.keyword.setText(temp);
			} else {
				holder.keyword.setText(txt);
			}
		}else{
			holder.keyword.setText(keywordList.getWords().get(position));
		}

		if (!isHasFooterView && position == getCount() - 1) {
			holder.divider.setVisibility(View.GONE);
		} else {
			holder.divider.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

}
